/**
 * Created by Administrator on 2017/9/21.
 */
angular.module('module.saleStatistics')
    .controller('callNumController', callNumController);
callNumController.$inject = ['$scope', "CallserviceList", '$state'];
function callNumController($scope, CallserviceList, $state, $localStorage) {

    var myDate = new Date();
    var vm = this;
    vm.openmodal = openmodal;
    vm.option = option;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    var shopid = $localStorage[environmental + "shopid"];

    if (CallserviceList == 'undefined') {
        vm.calllist = []
    } else {
        vm.calllist = CallserviceList;
    }
    var mystatime = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate() - 6, 0, 0, 0); // 默认七天前的时间
    vm.tur_statime = getstart(mystatime, 0);
    vm.tur_endtime = getstart(new Date(), 1); // 初始时间;
    var timelist = {
        statime: Date.parse(mystatime),
        endtime: Date.parse(new Date()),
    };

    getecharts(timelist.statime, timelist.endtime);

    $('#starttime').datetimepicker({
        format: "yyyy-MM-dd",
        autoclose: true,
        todayBtn: true,
        pickerPosition: "bottom-right",
        minView: 2,
        endDate: new Date(),
    }).on('changeDate', function (ev) {
        timelist.statime = jonsr(getstart(ev.date, 0));
        if (timelist.endtime == null || timelist.endtime == "") {
            $scope.$apply(function () {
                vm.tur_statime = getstart(ev.date, 0);
            })
        } else {
            if (timelist.statime > timelist.endtime) {
                vm.messageVisible = true;
                vm.message = "请输入正确的时间格式！";
                return;
            }
            $scope.$apply(function () {
                vm.tur_statime = getstart(ev.date, 0);
                getecharts(timelist.statime, timelist.endtime);
            })


        }
    });

    $('#endtime').datetimepicker({
        format: "yyyy-MM-dd",
        autoclose: true,
        todayBtn: true,
        pickerPosition: "bottom-right",
        minView: 2,
        endDate: new Date(),
    }).on('changeDate', function (ev) {
        timelist.endtime = jonsr(getstart(ev.date, 1));
        if (timelist.endtime == null || timelist.endtime == "") {
            $scope.$apply(function () {
                vm.tur_endtime = getstart(ev.date, 1);
            })
        } else {
            if (timelist.statime > timelist.endtime) {
                vm.messageVisible = true;
                vm.message = "请输入正确的时间格式！";
                return;
            }
            $scope.$apply(function () {
                vm.tur_endtime = getstart(ev.date, 1);
                getecharts(timelist.statime, timelist.endtime);
            })

        }
    });

    function getecharts(starttime, endtime) {
        vm.tableNum = [];
        var listecharts = [];//总数据
        var henglistecharts = [];//横坐标
        var zonglistecharts = [];//纵坐标
        for (var i = 0; i < vm.calllist.length; i++) {
            //vm.calllist[i].time = jonsr(echartime(vm.calllist[i].createtime,0));
            if (vm.calllist[i].time > starttime && vm.calllist[i].time < endtime) {
                /* listecharts.push(vm.calllist[i]);
                 zonglistecharts.push(vm.calllist[i].shopnumSum);
                 var data={};
                 data['time']=echartime(vm.calllist[i].date,1);
                 data['num']=vm.calllist[i].shopnumSum;
                 vm.tableNum.push(data);*/
            }
        }

        var data = ['加水', '纸巾', '牙签', '清桌'];
        var dataAxis = [{
            value: 5,
            itemStyle: {
                normal: {color: '#acd9ec'}
            }
        }, {
            value: 15,
            itemStyle: {
                normal: {color: '#6fc5e4'}
            }
        }, {
            value: 20,
            itemStyle: {
                normal: {color: '#63b3db'}
            }
        }, {
            value: 50,
            itemStyle: {
                normal: {color: '#3988c3'}
            }
        }];
        /*  var option = vm.option(data, dataAxis);
         var diagram = echarts.init(document.getElementById('tubiao'));
         diagram.setOption(option);*/
    }

    function openmodal() {
        $("#myModal").modal("show");
    }

    vm.type = 4;
    vm.img = img;
    function img(type) {
        vm.type = type;
        switch (type) {
            case 2:
                timelist.statime = new Date(new Date().getFullYear(), new Date().getMonth() - 1, new Date().getDate(), 0, 0, 0).getTime(); // 1个月前的时间戳
                timelist.endtime = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 23, 59, 59).getTime(); // 今天的时间戳
                getecharts(timelist.statime, timelist.endtime);
                break;
            case 3:
                timelist.statime = Date.parse(mystatime);
                timelist.endtime = Date.parse(new Date()); // 今天的时间戳
                getecharts(timelist.statime, timelist.endtime);
                break;
            case 4:
                timelist.statime = time_start = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), 0, 0, 0).getTime();
                timelist.endtime = Date.parse(new Date()); // 今天的时间戳
                getecharts(timelist.statime, timelist.endtime);
                break;
            default :
                timelist.statime = Date.parse(mystatime);
                timelist.endtime = Date.parse(new Date()); // 今天的时间戳
                getecharts(timelist.statime, timelist.endtime);
        }
    }

    function option(data, dataAxis) {
        return {
            grid: {
                x: 40,
                y: 30,
                x2: 30,
                y2: 5,
                borderWidth: 1
            },

            xAxis: {
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                },
                axisLabel: {
                    show: false
                }
            },
            yAxis: {
                type: 'category',
                data: data,
                min: 5,
                axisLabel: {
                    inside: false,
                    textStyle: {
                        fontSize: '10px',
                        color: 'black'
                    }
                },
                axisTick: {
                    show: false, // 隐藏坐标轴刻度

                },
                /*  axisLine: {
                 show: false // 隐藏坐标轴
                 },*/
                splitLine: {
                    show: false // 隐藏网格
                }
            },
            /*dataZoom: [
             {
             type: 'inside'
             }
             ],*/
            series: [
                {
                    type: 'bar',
                    barWidth: 20,

                    data: dataAxis,
                    barGap: '30%'
                },
            ]
        }
    };
}